<template>
  <div>
    <a-card :bordered="false" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="15">
              <a-form-item label="部门名称" >
                <a-input placeholder="请输入" v-model="queryParam.deptName"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="15">
              <a-form-item label="状态">
                <a-select placeholder="请选择" v-model="queryParam.status">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="true">正常</a-select-option>
                  <a-select-option value="false">停用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" >
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" @click="this.fetch" icon="search"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-button v-if="addEnable" type="primary" icon="plus-circle" shape="round" @click="$refs.drawer.add()">新建</a-button>
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 30px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
      </div>
      <a-table
        size="middle"
        ref="table"
        rowKey="id"
        :pagination="pagination"
        :loading="loading"
        :columns="columns"
        :dataSource="data"
        v-if="!loading"
        :defaultExpandAllRows="true">

        <span slot="status" slot-scope="status">
          <a-tag
            :key="status"
            :color="status === true ? 'green' : 'red'"
          >
            {{ status === true? '正常': '停用' }}
          </a-tag>
        </span>
        <span slot="customActionTitle"><a-icon type="form" :style="{ fontSize: '16px', color: '#4F99FF' }"/>  操作</span>

        <span slot="action" slot-scope="text, record">
          <a v-if="editEnable" @click="handleEdit(record)"><a-icon type="edit"/>  编辑</a>
          <a-divider type="vertical" />
          <a v-if="addEnable" @click="handleAdd(record.id)"><a-icon type="plus"/>  新增</a>
          <a-divider v-if="removeEnable && record.parentId !=='0'" type="vertical" />
          <a-popconfirm title="真的删除吗?" @confirm="delById(record.id)">
            <a v-if="removeEnable && record.parentId !=='0'"><a-icon type="delete"/>  删除</a>
          </a-popconfirm>
        </span>
      </a-table>

      <dept-modal ref="drawer" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { Table as T } from 'ant-design-vue'
import { checkPermission } from '@/utils/permissions'
import { findDeptList, delDept } from '@/api/system'
import { handleTree } from '@/utils/util'
import DeptModal from '../modules/DeptModal'
import message from 'ant-design-vue/es/message'
export default {
  name: 'DeptList',
  components: {
    T,
    DeptModal
  },
  data () {
    return {
      mdl: {},
      // 表头
      columns: [
        {
          title: '部门名称',
          dataIndex: 'deptName'
        },
        {
          title: '排序',
          dataIndex: 'orderNum',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          width: '300px',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          slots: { title: 'customActionTitle' }
        }
      ],
      // 查询参数
      queryParam: {},
      data: [],
      pagination: false,
      loading: false,
      addEnable: checkPermission('system:dept:add'),
      editEnable: checkPermission('system:dept:edit'),
      removeEnable: checkPermission('system:dept:remove'),
      hiddenSearch: false,
      icon: 'eye-invisible'
    }
  },
  filters: {
    statusFilter (status) {
      const statusMap = {
        false: '停用',
        true: '正常'
      }
      return statusMap[status]
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    handleOk () {
      this.fetch()
    },
    handleEdit (record) {
      this.$refs.drawer.edit(record)
    },
    handleAdd (id) {
      this.$refs.drawer.add(id)
    },
    fetch () {
      this.loading = true
      findDeptList(Object.assign(this.queryParam)).then(res => {
        this.data = handleTree(res.data, 'id')
        this.loading = false
      })
    },
    delById (id) {
      delDept({ deptId: id }).then(res => {
        this.fetch()
        message.success('部门删除成功')
      }).catch(res => {
        message.error(((res.response || {}).data || {}).message || res.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
